<template>
<div v-if="viewLoading" class="ivu-card ivu-card-bordered ivu-card-dis-hover">
  <div class="ivu-card-head"><p>{{ $route.meta.title }}</p></div>

  <div class="ivu-card-body">
    <iForm ref="formRef" :model="formFields" :rules="rules" :label-width="100">

      <FormItem prop="account" label="账号：">
        <Input v-model="formFields.account" placeholder="账号"/>
      </FormItem>

      <FormItem prop="password" label="密码：">
        <Input v-model="formFields.password" type="password" placeholder="密码"/>
      </FormItem>

      <FormItem prop="roles_id" label="角色：">
        <i-select v-model="formFields.roles_id">
          <i-option v-for="v in roles" :value="v.id.toString()" :key="v.id">{{ v.description }}</i-option>
        </i-select>
      </FormItem>

      <FormItem prop="status" label="状态：">
        <i-select v-model="formFields.status">
          <i-option v-for="(v, k) in apiConfig['backstage_account_status']" :value="k.toString()" :key="k">{{ v }}</i-option>
        </i-select>
      </FormItem>

      <FormItem>
        <Button type="primary" :loading="submitLoading" @click="onSubmit">{{ $route.meta.title }}</Button>
        <Button type="ghost" @click="$router.go(-1);">返回</Button>
      </FormItem>
    </iForm>
  </div>
</div>
</template>
<script>
import qs from 'qs';
import { Form, Input, Button, Select, Option, Message } from 'iview';
import ajaxiView from '@/utils/ajaxiView';
import apiConfig from '@/utils/apiConfig';
import auth from '@/utils/auth';

export default {
  components: {
    [Form.name]: Form,
    [Form.Item.name]: Form.Item,
    [Input.name]: Input,
    [Button.name]: Button,
    [Select.name]: Select,
    [Option.name]: Option
  },
  data() {
    return {
      token: auth.getToken(),
      apiConfig: {},
      roles: [],
      viewLoading: false,
      submitLoading: false,
      pAccount: '',
      formFields: {
        status: '0'
      },
      rules: {
        account: [
          {type:'string', required: true, message: '请输入账号!', trigger: 'blur'},
          {type:'string', min: 6, message: '请输入不少于6位的账号!', trigger: 'blur'}
        ],
        password: [
          {type:'string', required: true, message: '请输入密码!', trigger: 'blur'},
          {type:'string', min: 6, message: '请输入不少于6位的密码!', trigger: 'blur'}
        ],
        roles_id: [
          {type:'string', required: true, message: '请选择角色!', trigger: 'blur'}
        ],
        status: [
          {type:'string', required: true, message: '请选择状态!', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    _initialize() {
      var _this = this;
      apiConfig.getItem().then(function(api_config) {
        _this.apiConfig = api_config;
        _this.getRoles();
      });
    },
    getRoles() {
      let _this = this;
      ajaxiView.get('/backstage/account/roles?token='+_this.token).then(function(data) {
        if(data !== false && typeof(data.models) != 'undefined') {
          _this.roles = data.models;

          _this.viewLoading = true;
        }
     });
    },
    onSubmit() {
      var _this = this;
      _this.$refs.formRef.validate((valid) => {
        _this.submitLoading = true;
        if(valid) {
          ajaxiView.post('/backstage/account?token='+this.token, qs.stringify(_this.formFields)).then(function(data) {
            if(data !== false) {
              Message.success({
                content: data.msg,
                onClose: function() {
                  _this.$router.push({ name: 'backendAccountIndex' });
                }
              });
            }

            _this.submitLoading = false;
          });
        } else {
          _this.submitLoading = false;
        }
      });
    }
  },
  mounted() {
    this._initialize();
  }
}
</script>
